package br.com.seosoft.simplecalcfinan;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Senha extends ActivyGeral {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */

	private EditText senha, senha2, email;
	private Button botaoOk;
	private boolean senhaOk = false;
	private Spinner tempoInatividadeSpinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.senha);
		setTitle(R.string.definirSenha);

		
		

		email = (EditText) findViewById(R.senha.etEmail);
		senha = (EditText) findViewById(R.senha.etSenha);
		senha2 = (EditText) findViewById(R.senha.etSenha2);
		botaoOk = (Button) findViewById(R.senha.btnOk);
		tempoInatividadeSpinner = (Spinner) findViewById(R.senha.spinnerTempoInativo);

		botaoOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String senhaTexto, senhaTexto2, emailTexto;

				emailTexto = email.getText().toString();
				senhaTexto = senha.getText().toString();
				senhaTexto2 = senha2.getText().toString();
				SharedPreferences preference = PreferenceManager
						.getDefaultSharedPreferences(getApplicationContext());

				SharedPreferences.Editor editor = preference.edit();

				if (!senhaTexto.equals("") && !senhaTexto2.equals("")) {

					if (senhaTexto.equals(senhaTexto2)) {
						Toast.makeText(getApplicationContext(),
								"Senha definida com sucesso",
								Toast.LENGTH_SHORT).show();

						editor.putBoolean("senha", true);

						editor.putInt("tempo", getTempo());
						// editor.putString("email", emailTexto);
						editor.putString("password", senhaTexto);

						if (editor.commit()) {
							senhaOk = true;
							finish();
						}

					}
					if (!senhaTexto.equals(senhaTexto2)) {
						Toast.makeText(
								getApplicationContext(),
								getResources().getString(
										R.string.senhasNaoConferem),
								Toast.LENGTH_SHORT).show();
					}

				}

				if (senhaTexto.equals("") || senhaTexto == null) {
					senha.setError(getResources().getString(
							R.string.campoNaoNulo));
				}

				// if (emailTexto.equals("") || emailTexto == null
				// || !isEmailValid(emailTexto)) {
				// email.setError(getResources()
				// .getString(R.string.errorEmail));
				// }

				if (senhaTexto2.equals("") || senhaTexto2 == null) {
					senha2.setError(getResources().getString(
							R.string.campoNaoNulo));
				}

			}
		});

	}
	

	public int getTempo() {

		switch (tempoInatividadeSpinner.getSelectedItemPosition()) {

		case 0:

			return 30000;

		case 1:

			return 60000;

		case 2:

			return 120000;

		case 3:

			return 180000;

		case 4:

			return 300000;

		}
		return 30000;

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (senhaOk == false) {
			SharedPreferences preference = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());

			SharedPreferences.Editor editor = preference.edit();
			editor.putBoolean("senha", false);
			editor.commit();

		}

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (senhaOk == false) {
			SharedPreferences preference = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());

			SharedPreferences.Editor editor = preference.edit();
			editor.putBoolean("senha", false);
			editor.commit();

		}
		super.onPause();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (senhaOk == false) {
			SharedPreferences preference = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());

			SharedPreferences.Editor editor = preference.edit();
			editor.putBoolean("senha", false);
			editor.commit();

		}
		super.onBackPressed();
	}

	boolean isEmailValid(CharSequence email) {
		return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
	}
}
